package com.ddzh.web;

import com.ddzh.dto.UserDto;
import com.ddzh.entity.PageReq;
import com.ddzh.entity.PageRes;
import com.ddzh.entity.Result;
import com.ddzh.pojo.User;
import com.ddzh.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/userController")
public class UserController {

    @Reference
    private UserService userService;

    @PostMapping("/save")
    public Result save(@RequestBody UserDto userDto){
        return new Result(userService.saveOrUpdateUser(userDto));
    }

    @PostMapping("/findAll")
    public Result findAll(@RequestBody PageReq pageReq){
        return new Result(userService.findAll(pageReq));
    }
//    @GetMapping("/findAll")
//    public Result findAll(){
//        return new Result(userService.findAll(null));
//    }

    @PostMapping("/findByPage")
    public Result findByPage(@RequestBody PageReq pageReq){
        PageRes<User> pageRes = userService.findByPage(pageReq);
        return new Result(pageRes);
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return new Result(userService.updateById(user));
    }

    @GetMapping("/delete")
    public Result delete(Long id){
        return new Result(userService.deleteById(id));
    }

    @GetMapping("/modifyPassword/{id}/{password}")
    public Result modifyPassword(@PathVariable("id") Long userId,@PathVariable("password") String password){
        return new Result(userService.modifyPassword(userId,password));
    }
}
